<template>
  <div class="login-page">
    <!-- 背景装饰 -->
    <div class="login-bg"></div>
    
    <!-- 登录卡片 -->
    <div class="login-card">
      <!-- 标题区域 -->
      <div class="login-header">
        <div class="login-logo">
          <el-icon class="logo-icon"><Lock /></el-icon>
          <h1 class="logo-text">AI云盘登录</h1>
        </div>
        <p class="login-desc">请输入账号信息登录您的账户</p>
      </div>
      
      <!-- 登录表单 -->
      <el-form ref="formRef" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="phone" class="form-item">
          <el-input 
            v-model="loginForm.phone" 
            placeholder="请输入用户名"
            class="login-input"
            autocomplete="username"
          >
            <template #prefix>
              <el-icon class="input-icon"><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <el-form-item prop="password" class="form-item">
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="请输入密码" 
            show-password
            class="login-input"
            autocomplete="current-password"
          >
            <template #prefix>
              <el-icon class="input-icon"><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <!-- 操作按钮组 -->
        <div class="form-actions">
          <el-button 
            type="primary" 
            @click="handleSubmit" 
            :loading="loading" 
            class="login-btn"
          >
            <span v-if="!loading">登录</span>
            <el-icon v-else class="loading-icon"><Loading /></el-icon>
          </el-button>
          
          <el-button 
            type="default" 
            @click="guestLogin" 
            class="guest-btn"
          >
            使用游客账号登录
          </el-button>
        </div>
      </el-form>
      
      <!-- 底部辅助区域 -->
      <div class="login-footer">
        <div class="footer-row">
          <el-checkbox 
            v-model="rememberMe" 
            class="remember-checkbox"
          >
            下次自动登录
          </el-checkbox>
          
          <el-link 
            type="primary" 
            @click="toRegister"
            class="register-link"
          >
            注册账号
          </el-link>
        </div>
        
        <div class="security-info">
          <el-icon class="shield-icon"><Shield /></el-icon>
          <span class="security-text">账号信息加密传输，保障您的信息安全</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { useLoginUserStore } from "@/store/user";
import { Lock, User, Loading } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { reactive, ref } from "vue";
import type { FormInstance } from "element-plus";
import { useRouter, useRoute } from "vue-router";
import { base_url_main } from "@/api/routerToMain";

const loading = ref(false);
const rememberMe = ref(false);
const router = useRouter();
const route = useRoute();

const { login } = useLoginUserStore();

// 读取路由参数中的回跳地址
const redirectPath = ref(route.query.redirect as string || "/");

const loginRules = {
  phone: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
};

const formRef = ref<FormInstance>();

const loginForm = reactive({
  phone: "",
  password: "",
} as API.UserLoginRequest);

/** 跳转到注册页面 */
const toRegister = () => {
  window.location.href = base_url_main + "registerView";
};

/** 游客登录 */
const guestLogin = async () => {
  loginForm.phone = "13824122946";
  loginForm.password = "123456789";
  await handleSubmit();
};

/** 提交表单 */
const handleSubmit = async () => {
  if (!formRef.value) return;

  try {
    await formRef.value.validate();
  } catch (error) {
    ElMessage.error("请完善登录信息");
    return;
  }

  loading.value = true;
  try {
    await login(loginForm);
    router.push(redirectPath.value);
  } catch (error) {
    ElMessage.error("登录失败，请检查账号密码");
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
/* 页面容器 */
.login-page {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  box-sizing: border-box;
  background-color: #f5f7fa;
  position: relative;
  overflow: hidden;
}

/* 背景装饰 */
.login-bg {
  position: absolute;
  width: 100%;
  height: 100%;
  background: radial-gradient(circle at 50% 50%, rgba(64, 158, 255, 0.1) 0%, transparent 60%);
  z-index: 1;
}

/* 登录卡片 */
.login-card {
  width: 100%;
  max-width: 400px;
  background-color: #fff;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  padding: 36px 32px;
  box-sizing: border-box;
  position: relative;
  z-index: 2;
  transition: transform 0.3s ease;
}

.login-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.12);
}

/* 标题区域 */
.login-header {
  text-align: center;
  margin-bottom: 32px;
}

.login-logo {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-bottom: 12px;
}

.logo-icon {
  font-size: 32px;
  color: #409eff;
}

.logo-text {
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  margin: 0;
}

.login-desc {
  font-size: 14px;
  color: #909399;
  margin: 0;
}

/* 表单样式 */
.login-form {
  width: 100%;
}

.form-item {
  margin-bottom: 22px;
}

.login-input {
  height: 50px;
  border-radius: 8px;
  font-size: 15px;
  transition: all 0.3s ease;
}

:deep(.login-input .el-input__wrapper) {
  box-shadow: none !important;
  border: 1px solid #e5e7eb !important;
  border-radius: 8px !important;
  padding: 0 16px !important;
}

:deep(.login-input .el-input__wrapper:focus-within) {
  border-color: #409eff !important;
  box-shadow: 0 0 0 3px rgba(64, 158, 255, 0.1) !important;
}

.input-icon {
  color: #909399;
  font-size: 18px;
}

/* 按钮样式 */
.form-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 16px;
}

.login-btn {
  height: 50px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;
  background-color: #409eff;
  border-color: #409eff;
  transition: all 0.3s ease;
}

:deep(.login-btn:hover:not(.is-loading)) {
  background-color: #66b1ff;
  border-color: #66b1ff;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.25);
}

.loading-icon {
  animation: spin 1.5s linear infinite;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.guest-btn {
  height: 50px;
  font-size: 15px;
  border-radius: 8px;
  background-color: #f5f7fa;
  border-color: #e5e7eb;
  color: #606266;
  transition: all 0.3s ease;
}

:deep(.guest-btn:hover) {
  background-color: #eef3fa;
  border-color: #dcdfe6;
  color: #303133;
}

/* 底部区域 */
.login-footer {
  margin-top: 30px;
}

.footer-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

:deep(.remember-checkbox .el-checkbox__label) {
  font-size: 13px;
  color: #606266;
}

.register-link {
  font-size: 13px;
  color: #409eff;
  text-decoration: none;
  transition: color 0.2s ease;
}

:deep(.register-link:hover) {
  color: #66b1ff;
}

.security-info {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-size: 12px;
  color: #909399;
}

.shield-icon {
  font-size: 12px;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .login-card {
    padding: 28px 24px;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
  }

  .logo-icon {
    font-size: 28px;
  }

  .logo-text {
    font-size: 22px;
  }

  .login-header {
    margin-bottom: 26px;
  }

  .form-item {
    margin-bottom: 18px;
  }

  .login-input,
  .login-btn,
  .guest-btn {
    height: 46px;
  }

  .login-btn {
    font-size: 15px;
  }

  .guest-btn {
    font-size: 14px;
  }

  .login-footer {
    margin-top: 24px;
  }
}

@media (max-width: 480px) {
  .login-page {
    padding: 15px;
    background-color: #fff;
  }

  .login-card {
    box-shadow: none;
    padding: 20px 16px;
  }

  .footer-row {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }

  .register-link {
    align-self: flex-start;
  }
}
</style>